// 调试工具文件
const DEBUG_CONFIG = {
  // 是否启用调试模式
  enabled: true,
  
  // 日志级别
  logLevel: 'debug', // debug, info, warn, error
  
  // 是否显示网络请求详情
  showNetworkDetails: true
};

// 调试日志函数
const debugLog = {
  debug: (tag, message, data) => {
    if (DEBUG_CONFIG.enabled && DEBUG_CONFIG.logLevel === 'debug') {
      console.log(`[DEBUG][${tag}]`, message, data || '');
    }
  },
  
  info: (tag, message, data) => {
    if (DEBUG_CONFIG.enabled) {
      console.info(`[INFO][${tag}]`, message, data || '');
    }
  },
  
  warn: (tag, message, data) => {
    if (DEBUG_CONFIG.enabled) {
      console.warn(`[WARN][${tag}]`, message, data || '');
    }
  },
  
  error: (tag, message, data) => {
    if (DEBUG_CONFIG.enabled) {
      console.error(`[ERROR][${tag}]`, message, data || '');
    }
  }
};

// 网络请求调试
const networkDebug = {
  logRequest: (url, method, data) => {
    if (DEBUG_CONFIG.showNetworkDetails) {
      debugLog.debug('NETWORK', `请求: ${method} ${url}`, data);
    }
  },
  
  logResponse: (url, response) => {
    if (DEBUG_CONFIG.showNetworkDetails) {
      debugLog.debug('NETWORK', `响应: ${url}`, response);
    }
  },
  
  logError: (url, error) => {
    if (DEBUG_CONFIG.showNetworkDetails) {
      debugLog.error('NETWORK', `错误: ${url}`, error);
    }
  }
};

// 系统信息调试
const systemDebug = {
  logSystemInfo: () => {
    try {
      const systemInfo = wx.getSystemInfoSync();
      debugLog.info('SYSTEM', '系统信息', {
        platform: systemInfo.platform,
        system: systemInfo.system,
        version: systemInfo.version,
        SDKVersion: systemInfo.SDKVersion,
        brand: systemInfo.brand,
        model: systemInfo.model
      });
    } catch (error) {
      debugLog.error('SYSTEM', '获取系统信息失败', error);
    }
  },
  
  checkNetworkStatus: () => {
    wx.getNetworkType({
      success: (res) => {
        debugLog.info('NETWORK', '网络状态', {
          networkType: res.networkType,
          isConnected: res.networkType !== 'none'
        });
      },
      fail: (error) => {
        debugLog.error('NETWORK', '获取网络状态失败', error);
      }
    });
  }
};

// 登录调试工具
const loginDebug = {
  logLoginStart: () => {
    debugLog.info('LOGIN', '开始登录流程');
    systemDebug.logSystemInfo();
    systemDebug.checkNetworkStatus();
  },
  
  logWxLoginResult: (result) => {
    debugLog.info('LOGIN', '微信登录结果', result);
  },
  
  logApiCall: (params) => {
    debugLog.info('LOGIN', 'API调用参数', params);
  },
  
  logApiResponse: (response) => {
    debugLog.info('LOGIN', 'API响应', response);
  },
  
  logLoginError: (error, context) => {
    debugLog.error('LOGIN', `登录错误 - ${context}`, error);
  }
};

module.exports = {
  DEBUG_CONFIG,
  debugLog,
  networkDebug,
  systemDebug,
  loginDebug
};